package com.example.boos.login;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.boos.R;


public class LoginFragment extends Fragment {

    private EditText mUserName;
    private EditText mPassword;
    private Button mLogin;
    private Button mRegister;
    private LoginFragmentCallBack myCallBack;

    private View view;

    public EditText getmUserName(){
        return mUserName;
    }
    public EditText getmPassword(){
        return mPassword;
    }

    public LoginFragment(){}
    public LoginFragment(LoginFragmentCallBack callBack){
        myCallBack = callBack;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

         view = inflater.inflate(R.layout.fragment_login,container,false);
        return view;
    }

    // 添加事件监听
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mUserName = view.findViewById(R.id.myUserName); // 其实是电话号码
        mPassword = view.findViewById(R.id.myPassword);
        mLogin = view.findViewById(R.id.LoginBtn);
        mRegister = view.findViewById(R.id.RegisterBtn);
        mUserName.addTextChangedListener(new TextWatcher() {
            // 文本输入完毕后
            @Override
            public void afterTextChanged(Editable s) {
                Log.e("文本输入完毕后",s.toString());
                myCallBack.onTelephoneChanged(s.toString());
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
        });
        mPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                myCallBack.onPasswordChanged(s.toString());
            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
        });

        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myCallBack.LoginBtn();
            }
        });
        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myCallBack.Register();
            }
        });

    }
}
